var dengluBox1 = document.getElementById("dengluBox1")
var loginBox = document.getElementById("loginBox")
const service = "/api/Questionnaire/v1/"

function yidenglu(){ // 右上角登录标志
	if (dengluBox1.style.display == 'block'){
		dengluBox1.style.display = 'none'
	} else {
		dengluBox1.style.display = "block"
	}
}
//获取用户基本信息
function getUserInfo(){
	$.ajax({
		type: "get",
		url: "/api/jiali_api/v1/user/select",
		beforeSend: function(xhr) {
			xhr.setRequestHeader("Authorization", "Bearer "+$.cookie("token"));
		},
		success: function(res){

				$.cookie("userInfo", JSON.stringify(res), { expires:604800});
				userInfo();
		}
	});
}
//判断用户登录状态
function userInfo(){
	if($.cookie('userInfo')){
		// console.log(JSON.parse($.cookie('userInfo')).name)
		if(JSON.parse($.cookie('userInfo')).name){
			var pattern1 = /^[\u4e00-\u9fa5]{1}/g;
			var name = JSON.parse($.cookie('userInfo')).name.match(pattern1)[0]+"**"
			$('#userInfoName').html(name)
			localStorage.setItem('name', name)
		}else{
			var pattern1 = /^(\d{3})\d{4}(\d{4})$/;
			console.log(JSON.parse($.cookie('userInfo')));
			var phone = JSON.parse($.cookie('userInfo')).phone.replace(pattern1, '$1****$2')
			
			$('#userInfoName').html(phone)
			localStorage.setItem('phone', phone)
			
		}
		document.getElementsByClassName('yidenglu')[0].style.display = 'block'
		document.getElementsByClassName('loginBox')[0].style.display = 'none'
	}
}
userInfo()
function liveOut(){ // 退出登录
	var yidenglu = document.getElementById("yidenglu")
	yidenglu.style.display = 'none'
	loginBox.style.display = 'flex'
	$.cookie("userInfo", "", { expires:0});
	remove.localStorage('token')
	localStorage.removeItem('name')
	localStorage.removeItem('phone')
	userInfo()
}
var zhanghaologin = document.getElementById('zhanghaologin')
var fastlogin = document.getElementById('fastlogin')
var denglubox = document.getElementById('denglubox')
var zhucebox = document.getElementById('zhucebox')
function dengluTabAc(){// 快捷登录
	zhanghaologin.style.display="block";
	fastlogin.style.display="none";
	document.getElementsByClassName('fastbg')[0].style.backgroundColor = "#fff";
	document.getElementsByClassName('zhanghaobg')[0].style.backgroundColor = "#eceff4";
}
function loginBt(){// 点击快捷登录的登录按钮
	var fastlogin = document.forms["loginForm"]["phone"].value;
	var fastpassword = document.forms["loginForm"]["password"].value;
	if (!(/^1[3456789]\d{9}$/.test(fastlogin))){
		alert('手机号不正确')
	}else if(fastpassword =="" || fastpassword.length<5){
		alert('密码不正确')
	}else{
		var data = {'phone':fastlogin,'password':fastpassword};
		$.post(service+"front_user/front_login",data,function(res){
				if(res.status_code=="200"){
					document.getElementsByClassName('loginBox')[0].style.display = 'none'
					document.getElementsByClassName('yidenglu')[0].style.display = 'block'
					denglubox.style.display="none";
					$.cookie("token", res.data.token, { expires: res.data.expires_in});
					localStorage.setItem('topins',0)
					localStorage.setItem('token', res.data.token) // 存储token
					
					localStorage.setItem('isLogin', true)
					getUserInfo();   
				}else{
					alert(res.message);
				}
        });
	}
}
var TIME_COUNTregist = 60 
var registFormtimer = null // 注册倒计时
var registFormcount = '' // 注册倒计时60秒
function getregistYan(){ // 注册弹框发送验证码
	var registlogin = document.forms["registForm"]["phone"].value;
	if (!(/^1[3456789]\d{9}$/.test(registlogin))){
		alert('手机号不正确')
	}else{
		data = {'phone':registlogin,'type':1}
		$.post(service+"front_user/phone_code",data,function(res){
			if(res.status_code!="200"){
				return
			}
		})
		document.getElementById('registweifasong').style.display = 'none'
		document.getElementById('registyifasong').style.display = 'block'
		if (!this.registFormtimer) {
			this.registFormcount = TIME_COUNTregist;
			// this.registFormregistYan = false;
			this.registFormtimer = setInterval(() => {
			if (this.registFormcount > 0 && this.registFormcount <= TIME_COUNTregist) {
				this.registFormcount--;
				document.getElementById('registNum').innerText = this.registFormcount
				} else {
				document.getElementById('registweifasong').style.display = 'block'
				document.getElementById('registyifasong').style.display = 'none'
				clearInterval(this.registFormtimer);
				this.registFormtimer = null;
				}
			}, 1000)
		}
	}
	
}
var ischecked = false
function changeSelectAc(){ // 判断是否阅读协议
	var uncheckedFlag = document.getElementById('unchecked')
	var checkedFlag = document.getElementById('checked')
	if(checkedFlag.style.display == "none"){
		this.ischecked = true
		checkedFlag.style.display = "block"
		uncheckedFlag.style.display = "none"
	} else {
		this.ischecked = false
		checkedFlag.style.display = "none"
		uncheckedFlag.style.display = "block"
	}
}
function registAction(){ // 注册确认按钮
	var registlogin = document.forms["registForm"]["phone"].value;
	var registpassword = document.forms["registForm"]["password"].value;
	var registcode = document.forms["registForm"]["code"].value;
	if (!(/^1[3456789]\d{9}$/.test(registlogin))){
		alert('手机号不正确')
	}else if(registpassword==null || registpassword.length<6){
		alert("密码长度至少为6位数")
	} else if (!(/^\d{6}$/.test(registcode))){
		alert('验证码有误，请重新填写')
	} else if(!ischecked){
		alert('请先阅读蜗牛家事使用协议')
	} else {

		var data = {'phone':registlogin,'password':registpassword,'code':registcode};
		$.post(service+"front_user/add_user",data,function(res){
				console.log(res)
				if(res.status_code=="200"){
					document.getElementsByClassName('unregist')[0].style.display = 'none'
					document.getElementsByClassName('registsuccess')[0].style.display = 'block'
					$.cookie("token", res.data.token, { expires: res.data.expires_in});
					localStorage.setItem('topins',0)
					localStorage.setItem('token', res.data.token) // 存储token
					localStorage.setItem('isLogin', true)
					getUserInfo();
					userInfo();
				}else{
					alert(res.message);
				}
		});

	}
}
function zhanghaoAc(){// 密码登录
	zhanghaologin.style.display="none";
	fastlogin.style.display="block";
	document.getElementsByClassName('fastbg')[0].style.backgroundColor = "#eceff4";
	document.getElementsByClassName('zhanghaobg')[0].style.backgroundColor = "#fff";
}
var TIME_COUNT = 60 
var loginFormtimer = null // 快捷登录倒计时
var loginFormcount = '' // 快捷登录倒计时60秒
function loginFormgetYan(){ // 登录弹框发送验证码
	var fastlogin = document.forms["loginForm"]["zhanghao"].value;
	if (!(/^1[3456789]\d{9}$/.test(fastlogin))){
		alert('手机号不正确')
	}else {
	document.getElementById('loginweifasong').style.display = 'none'
	document.getElementById('loginyifasong').style.display = 'block'
	data = {'phone':fastlogin,'type':5}
	$.post(service+"front_user/phone_code",data,function(res){
		if(res.status_code!="200"){
			return
		}
	})
	if (!this.loginFormtimer) {
	this.loginFormcount = TIME_COUNT;
	// this.loginFormregistYan = false;
	this.loginFormtimer = setInterval(() => {
		if (this.loginFormcount > 0 && this.loginFormcount <= TIME_COUNT) {
			this.loginFormcount--;
			document.getElementById('loginNum').innerText = this.loginFormcount
		} else {
			document.getElementById('loginweifasong').style.display = 'block'
			document.getElementById('loginyifasong').style.display = 'none'
			clearInterval(this.loginFormtimer);
			this.loginFormtimer = null;
		}
	}, 1000)
	}
	}
}
function goRegistAction() {// 注册账号
	denglubox.style.display="none";
	zhucebox.style.display="block";
}
// 账号登录按钮
function fastloginBt(){
	var fastzhanghao = document.forms["loginForm"]["zhanghao"].value;
	var fastcode = document.forms["loginForm"]["code"].value;
	if (!(/^1[3456789]\d{9}$/.test(fastzhanghao))){
		alert('手机号不正确')
	}else if(fastcode=="" || fastcode.length<6){
		alert('验证码不正确')
	}else{
		var data = {'phone':fastzhanghao,'code':fastcode};
		$.post(service+"front_user/code_login_phone",data,function(res){
				console.log(res)
				if(res.status_code=="200"){
					document.getElementsByClassName('loginBox')[0].style.display = 'none'
					document.getElementsByClassName('yidenglu')[0].style.display = 'block'
					denglubox.style.display="none";
					$.cookie("token", res.data.token, { expires: res.data.expires_in});
					localStorage.setItem('topins',0)
					localStorage.setItem('token', res.data.token) // 存储token
					localStorage.setItem('isLogin', true)
					getUserInfo();
				}else{
					alert(res.message);
				}
        });
	}
}
function closeLoginDialog(){  // 关闭登录弹窗
	denglubox.style.display="none";
}
function loginAc(){ // 打开登录弹窗
	denglubox.style.display="block";
	document.forms["loginForm"]["phone"].value = ''
	document.forms["loginForm"]["zhanghao"].value = ''
	document.forms["loginForm"]["password"].value = ''
	document.forms["loginForm"]["code"].value = ''
}
function registAc() { // 打开注册弹窗
	zhucebox.style.display="block";
	document.forms["registForm"]["phone"].value = ''
	document.forms["registForm"]["password"].value = ''
	document.forms["registForm"]["code"].value = ''
}
function tishibox(){ // 点击提示框
	var yuanyin = document.getElementById("yuanyin")
	if(yuanyin.style.display == "none"){
		yuanyin.style.display = "block"
	} else {
		yuanyin.style.display = "none"
	}
}
function xiufuzhanghao(){//修复账号
	var xiufuBox = document.getElementById("xiufuBox")
	if(xiufuBox.style.display == "none"){
		xiufuBox.style.display = "block"
	} else {
		xiufuBox.style.display = "none"
	}
}
function closeforgetDialog2(){ // 关闭重置密码弹窗2
	resetpasswordBox2.style.display = 'none'
}
function ungetcodeAction(){// 重置密码弹窗2收不到验证码
	var ungetcode = document.getElementById("ungetcode")
	if(ungetcode.style.display == "none"){
		ungetcode.style.display = "block"
	} else {
		ungetcode.style.display = "none"
	}
}
function closeDialog() { // 关闭注册弹窗
	zhucebox.style.display="none";
}
function goregist(){ // 注册
	denglubox.style.display="none";
	zhucebox.style.display="block";
}
function goLoginAction(){ 
	denglubox.style.display="block";
	zhucebox.style.display="none";
}
var resetpasswordBox1 = document.getElementById("resetpasswordBox1")
var resetpasswordBox2 = document.getElementById("resetpasswordBox2")
function forgetAc(){ // 点击忘记密码
	resetpasswordBox1.style.display = 'block'
	denglubox.style.display="none";
	document.forms["resetpasswordForm"]["phone"].value = ''
}
function forgetNext(){ // 重置密码弹窗1下一步
	var registlogin = document.forms["resetpasswordForm"]["phone"].value;
	if (!(/^1[3456789]\d{9}$/.test(registlogin))){
		alert('手机号不正确')
	}else {
		document.forms["resetpasswordForm2"]["code"].value = ''
		resetpasswordBox1.style.display = 'none'
		resetpasswordBox2.style.display = 'block'
		document.getElementById('resetpasswordBox2Num').innerText = registlogin
	}
	
}
function closeforgetDialog(){// 关闭重置密码弹窗
	resetpasswordBox1.style.display = 'none'
}
var resetpasswordFormtimer = null // 快捷登录倒计时
var resetpasswordFormcount = '' // 快捷登录倒计时60秒
function findYan(){ // 重置密码弹框2获取验证码
	document.getElementById('resetpasswordweiyanzheng').style.display = 'none'
	document.getElementById('resetpasswordyiyanzheng').style.display = 'block'
	var registlogin = document.forms["resetpasswordForm"]["phone"].value;
	data = {'phone':registlogin,'type':3}
	$.post(service+"front_user/phone_code",data,function(res){
		if(res.status_code!="200"){
			return
		}
	})
	if (!this.resetpasswordFormtimer) {
	  this.resetpasswordFormcount = TIME_COUNT;
	  // this.loginFormregistYan = false;
	  this.resetpasswordFormtimer = setInterval(() => {
	  if (this.resetpasswordFormcount > 0 && this.resetpasswordFormcount <= TIME_COUNT) {
			 this.resetpasswordFormcount--;
			 document.getElementById('resetpasswordNum').innerText = this.resetpasswordFormcount
			} else {
			 document.getElementById('resetpasswordweiyanzheng').style.display = 'block'
			 document.getElementById('resetpasswordyiyanzheng').style.display = 'none'
			 clearInterval(this.resetpasswordFormtimer);
			 this.resetpasswordFormtimer = null;
			}
	  }, 1000)
	 }
}
function resetPrev(){ // 忘记密码弹窗2上一步
	resetpasswordBox1.style.display = 'block'
	resetpasswordBox2.style.display = 'none'
}
function resetnext(){// 忘记密码弹窗2下一步
	var registcode = document.forms["resetpasswordForm2"]["code"].value;
	if(registcode=='' || registcode.length<6){
		alert("验证码不正确")
	} else {
		var registlogin = document.forms["resetpasswordForm"]["phone"].value;
		data = {'phone':registlogin,'type':3,'code':registcode}
		$.post(service+"front_user/phone_code_v",data,function(res){
			if(res.status_code!="200"){
				alert(res.message);
				return 
			}else{
				resetpasswordBox2.style.display = 'none'
		document.forms["resetpasswordForm3"]["password"].value = "";
		document.forms["resetpasswordForm3"]["passwordAgain"].value = ""
		resetpasswordBox3.style.display = 'block'
			}
		})
		

	}
}
function closePhonePw() { // 关闭忘记密码弹窗3
	resetpasswordBox3.style.display = 'none'
}
function resetPWprev() { // 忘记密码弹窗3上一步
	resetpasswordBox3.style.display = 'none'
	resetpasswordBox2.style.display = 'block'
}
function resetPW(){ // 忘记密码弹窗3完成
	var resetpassword = document.forms["resetpasswordForm3"]["password"].value;
	var resetpasswordagain = document.forms["resetpasswordForm3"]["passwordAgain"].value;
	if(resetpassword =='' || resetpasswordagain ==''){
		alert('密码不能为空')
	} else  if(resetpassword !==resetpasswordagain){
		alert('两次密码输入不一致')
	}else if (resetpassword.length<6 || resetpasswordagain.length<6){
		alert('密码不能少于6位数')
	} else {
		var registlogin = document.forms["resetpasswordForm"]["phone"].value;
		data = {'phone':registlogin,'password':resetpassword,'passwordagain':resetpasswordagain}
		$.post(service+"front_user/update_password_phone",data,function(res){
			if(res.status_code!="200"){
				alert(res.message)
			}
		})
		resetpasswordBox3.style.display = 'none'
		alert('密码重置成功')
	}
}

// function getQueryVariable(variable)
// {
// 	var query = window.location.search.substring(1);
// 	var vars = query.split("&");
// 	for (var i=0;i<vars.length;i++) {
// 			var pair = vars[i].split("=");
// 			if(pair[0] == variable){return pair[1];}
// 	}
// 	return(false);
// }

function searchAction(){
	var title = $("#select").val();
	window.location.href="/news/select_os_blade_news?title="+title;
}

function page_no(){
	var res = $(".el-pager").li;
	console.log(res)
}
page_no()